"""add memory_nodes and memory_edges tables"""

from alembic import op
import sqlalchemy as sa


revision = "202510180003"
down_revision = "202510180002"
branch_labels = None
depends_on = None


def upgrade() -> None:
  op.create_table(
    "memory_nodes",
    sa.Column("id", sa.Integer(), primary_key=True, index=True),
    sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
    sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
    sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
    sa.Column("user_id", sa.Integer(), nullable=False, index=True),
    sa.Column("relationship_id", sa.Integer(), nullable=True, index=True),
    sa.Column("type", sa.String(length=32), nullable=False, index=True),
    sa.Column("label", sa.String(length=255), nullable=False, index=True),
    sa.Column("props_json", sa.JSON(), nullable=False),
    sa.ForeignKeyConstraint(["relationship_id"], ["relationships.id"], ondelete="SET NULL"),
    sa.UniqueConstraint("user_id", "relationship_id", "type", "label", name="uq_memory_nodes_scope"),
  )

  op.create_table(
    "memory_edges",
    sa.Column("id", sa.Integer(), primary_key=True, index=True),
    sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
    sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
    sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
    sa.Column("user_id", sa.Integer(), nullable=False, index=True),
    sa.Column("relationship_id", sa.Integer(), nullable=True, index=True),
    sa.Column("src_id", sa.Integer(), nullable=False, index=True),
    sa.Column("dst_id", sa.Integer(), nullable=False, index=True),
    sa.Column("type", sa.String(length=32), nullable=False, index=True),
    sa.Column("props_json", sa.JSON(), nullable=False),
    sa.ForeignKeyConstraint(["relationship_id"], ["relationships.id"], ondelete="SET NULL"),
    sa.ForeignKeyConstraint(["src_id"], ["memory_nodes.id"], ondelete="CASCADE"),
    sa.ForeignKeyConstraint(["dst_id"], ["memory_nodes.id"], ondelete="CASCADE"),
    sa.UniqueConstraint("user_id", "relationship_id", "type", "src_id", "dst_id", name="uq_memory_edges_scope"),
  )


def downgrade() -> None:
  op.drop_table("memory_edges")
  op.drop_table("memory_nodes")

